package com.demo.email.service.impl;

import com.demo.email.service.MailService;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 邮件服务
 *
 * @author yueyang
 * @since 2022-06-24 16:31:00
 */
@Service
@RequiredArgsConstructor
public class MailServiceImpl implements MailService {

  private final JavaMailSender javaMailSender;

  @Value("${spring.mail.username}")
  private String from;

  /**
   * 发送文本邮件
   *
   * @param to 收件人地址
   * @param subject 邮件主题
   * @param content 邮件内容
   * @param cc 抄送地址
   */
  @Override
  public void sendSimpleMail(String to, String subject, String content, String... cc) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo(to);
    message.setSubject(subject);
    message.setText(content);
    if (!ObjectUtils.isEmpty(cc)) {
      message.setCc(cc);
    }
    javaMailSender.send(message);
  }

  /**
   * 发送HTML邮件
   *
   * @param to 收件人地址
   * @param subject 邮件主题
   * @param content 邮件内容
   * @param cc 抄送地址
   * @throws MessagingException 邮件发送异常
   */
  @Override
  public void sendHtmlMail(String to, String subject, String content, String... cc)
      throws MessagingException {
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(content, true);
    if (!ObjectUtils.isEmpty(cc)) {
      helper.setCc(cc);
    }
    javaMailSender.send(message);
  }

  /**
   * 发送带附件的邮件
   *
   * @param to 收件人地址
   * @param subject 邮件主题
   * @param content 邮件内容
   * @param filePath 附件地址
   * @param cc 抄送地址
   * @throws MessagingException 邮件发送异常
   */
  @Override
  public void sendAttachmentsMail(
      String to, String subject, String content, String filePath, String... cc)
      throws MessagingException {
    MimeMessage message = javaMailSender.createMimeMessage();

    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(content, true);
    if (!ObjectUtils.isEmpty(cc)) {
      helper.setCc(cc);
    }
    FileSystemResource file = new FileSystemResource(new File(filePath));
    String fileName = FilenameUtils.getName(filePath);
    helper.addAttachment(fileName, file);

    javaMailSender.send(message);
  }

  /**
   * 发送正文中有静态资源的邮件
   *
   * @param to 收件人地址
   * @param subject 邮件主题
   * @param content 邮件内容
   * @param rscPath 静态资源地址
   * @param rscId 静态资源id
   * @param cc 抄送地址
   * @throws MessagingException 邮件发送异常
   */
  @Override
  public void sendResourceMail(
      String to, String subject, String content, String rscPath, String rscId, String... cc)
      throws MessagingException {
    MimeMessage message = javaMailSender.createMimeMessage();

    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(content, true);
    if (!ObjectUtils.isEmpty(cc)) {
      helper.setCc(cc);
    }
    FileSystemResource res = new FileSystemResource(new File(rscPath));
    helper.addInline(rscId, res);

    javaMailSender.send(message);
  }
}
